<?php
/**
 * CompleXml Framework
 *
 * @category   CompleXml
 * @package    CompleXml_Object
 * @copyright  Copyright (c) 2008 Andrey Kucherenko
 * @license    New BSD License
 * @version    $Id: Cli.php 8 2008-11-13 14:50:42Z kucherenko.andrey $
 */
require_once 'CompleXml/Log.php';
require_once 'CompleXml/Object/Abstract.php';
require_once 'CompleXml/Object/Cli/Exception.php';
class CompleXml_Object_Cli extends CompleXml_Object_Abstract
{
    private $_programm_name;
    private $_process_count;
    private $_process_name;
    private $_plock_file = null;
    private $_plocks_dir;
    /**
     * @return string
     */
    public function getPlocksDir ()
    {
        return $this->_plocks_dir;
    }
    /**
     * @param string $plocks_dir
     */
    public function setPlocksDir ($plocks_dir)
    {
        $this->_plocks_dir = $plocks_dir;
    }
    public function __construct ()
    {}
    /**
     * @return integer
     */
    public function getProcessCount ()
    {
        return $this->_process_count;
    }
    /**
     * @return string
     */
    public function getProcessName ()
    {
        return $this->_process_name;
    }
    /**
     * @return string
     */
    public function getProgrammName ()
    {
        return $this->_programm_name;
    }
    /**
     * @param integer $process_count
     */
    public function setProcessCount ($process_count)
    {
        $this->_process_count = $process_count;
    }
    /**
     * @param string $process_name
     */
    public function setProcessName ($process_name)
    {
        $this->_process_name = $process_name;
    }
    /**
     * @param string $programm_name
     */
    public function setProgrammName ($programm_name)
    {
        $this->_programm_name = $programm_name;
    }
    public function start ($programm_name, $process_count, $plock_dir = '/tmp')
    {
        $this->setProgrammName($programm_name);
        $this->setProcessCount($process_count);
        $this->setPlocksDir($plock_dir);
        for ($i = 1; $i <= $this->_process_count; $i ++) {
            $this->setProcessName($this->getProgrammName() . "_" . $i);
            $this->_plock_file = @fopen($this->getPlocksDir() . '/' . $this->getProcessName() . ".lock", "a");
            if (! @flock($this->_plock_file, LOCK_NB | LOCK_EX)) {
                @fclose($this->_plock_file);
                continue;
            }
            CompleXml_Log::add('Process named ' . $this->getProcessName() . ' started', CompleXml_Log::INFO);
            return true;
        }
        CompleXml_Log::add('Can\'t start process, proccess already started (' . $this->getProcessName() . ')', CompleXml_Log::ERR);
        throw new CompleXml_Object_Cli_Exception('Can\'t start process, proccess already started (' . $this->getProcessName() . ')');
    }
    public function finish ()
    {
        if (! is_null($this->_plock_file)) {
            flock($this->_plock_file, LOCK_UN);
            fclose($this->_plock_file);
            CompleXml_Log::add('Process named ' . $this->getProcessName() . ' finished', CompleXml_Log::INFO);
        }
    }
    public function execute ()
    {}
}